package com.demo.cms.action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.demo.common.StringUtils;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 页面登陆Action
 * @author ryuu.kk
 *
 */
public class LoginAction extends ActionSupport implements SessionAware {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = -7193903795850722548L;

    /** session **/
    private Map<String, Object> session;
    
	/**
	 * 首页登陆页面
	 * @return String
	 */
	public String execute() {
		
		if (session != null) {
    		Object obj = session.get("SPRING_SECURITY_LAST_EXCEPTION");
    		if (obj != null && obj instanceof Exception) {
    			String errorMessage = ((Exception) obj).getMessage();
    			// 由于SpringSecurity对一些异常消息没有做处理,则getMessage为空
    			if (StringUtils.isEmpty(errorMessage)) {
    				errorMessage = ((Exception) obj).getCause().toString();
    			}
    			this.addActionError(errorMessage);
    			session.remove("SPRING_SECURITY_LAST_EXCEPTION");
    		}
    	}
		
		return SUCCESS;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
}
